package com.lyn.auxiliarytool.presenters;

import android.content.Context;

import com.lyn.auxiliarytool.contract.BaseAsync;
import com.lyn.auxiliarytool.contract.Contract;
import com.lyn.auxiliarytool.contract.Result;
import com.lyn.auxiliarytool.contract.ThreadPool;
import com.lyn.auxiliarytool.models.InitModel;

import java.lang.ref.WeakReference;

/**
 * @author Lyn
 * @version 1.0.0 2022/11/29
 */
public class InitPresenter implements Contract.Presenters.InitPresenter {

    private final InitModel initModel = new InitModel();
    private final WeakReference<Contract.Views.ViewLoading> viewLoadingWeakReference;

    public InitPresenter(Contract.Views.ViewLoading viewLoading) {
        this.viewLoadingWeakReference = new WeakReference<>(viewLoading);
    }

    @Override
    public void init(Context context) {
        Contract.Views.ViewLoading viewLoading = viewLoadingWeakReference.get();
        new BaseAsync<Void, Integer, Result>() {
            @Override
            public void onPreExecute() {
                super.onPreExecute();
                viewLoading.onLoading();
            }

            @Override
            public Result doInBackground(Void... voids) {
                return initModel.init(context);
            }

            @Override
            public void onPostExecute(Result result) {
                super.onPostExecute(result);
                viewLoading.onHideLoading();
                viewLoading.onResult(result);
            }
        }.execute(ThreadPool.get());
    }
}
